<?php

namespace app\api\library\index\addUserAddress;

use think\facade\Db;

/**
 *
 * User: chenzheng
 * Date: 2023/3/21
 * Time: 15:11
 */
class V
{
    public static function run($param)
    {
        
        $userId      = _checkEmptyParam($param[ 'userId' ], '用户ID不能为空');
        $name        = _checkEmptyParam($param[ 'name' ], '用户姓名不能为空');
        $phone       = _checkEmptyParam($param[ 'phone' ], '用户电话不能为空');
        $province_id = _checkEmptyParam($param[ 'province_id' ], '省不能为空');
        $province    = _checkEmptyParam($param[ 'province' ], '省不能为空');
        $city        = _checkEmptyParam($param[ 'city' ], '市不能为空');
        $city_id     = _checkEmptyParam($param[ 'city_id' ], '市不能为空');
        $region_id   = _checkEmptyParam($param[ 'region_id' ], '区不能为空');
        $region      = _checkEmptyParam($param[ 'region' ], '区不能为空');
        $detail      = _checkEmptyParam($param[ 'detail' ], '详细地址不能为空');
        if (Db::name('mall_address')->where([['id', '=', $userId], ['delete_time', '=', null]])->count() >= 5) {
            _outPut(204, '', '您只能添加五个收货地址');
        }
        $addData = [
            'user_id'          => $userId,
            'name'        => $name,
            'phone'       => $phone,
            'province_id' => $province_id,
            'province'    => $province,
            'city_id'     => $city_id,
            'city'        => $city,
            'region_id'   => $region_id,
            'region'      => $region,
            'detail'      => $detail,
            'create_time' => time(),
            'update_time' => time(),
        ];
        if (false === Db::name('mall_address')->insert($addData)) {
            _outPut(204, '', '添加失败');
        }
        _outPut(200);
    }
}